Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.WX_VideoMgr = undefined;
var $2AudioManager = require("AudioManager");
var $2GameUser = require("GameUser");
var $2EventCenter = require("EventCenter");
var $2GameEvent = require("GameEvent");
var $2MiniPlatform = require("MiniPlatform");
var exp_WX_VideoMgr = /* */function () {
  function _ctor() {
    babel_classCallCheck(this, _ctor);
    this._rewardVideoAd = new Map();
    this._rewardcomplat = new Map();
    this._showing = false;
  }
  return babel_createClass(_ctor, [{
    key: "Create",
    value: function Create(e, t) {
      var _this151 = this;
      this._rewardcomplat.set(e, t);
      var n = this;
      if (!this._rewardVideoAd.has(e)) {
        console.log("创建视频", e);
        var _t73 = window.wx.createRewardedVideoAd({
          adUnitId: e,
          multiton: true
        });
        _t73.videoid = e;
        _t73.onError(function (i) {
          console.log(i.errCode);
          n._showing = false;
          cc.game.resume();
          $2AudioManager.AudioManager.getInstance().resumeAll();
          var s = n._rewardcomplat.get(e);
          null != s && s(i.errCode);
          _this151.reportRewardBehavior(_t73, 5);
        });
        _t73.onLoad(function (e) {
          console.log("onLoad------------------", e);
          if (null != e && null != e) {
            _t73.shareValue = e.shareValue;
            _t73.rewardValue = e.rewardValue;
            $2EventCenter.default.postEvent(_t73.videoid);
          }
        });
        _t73.onClose(function (s) {
          n._showing = false;
          $2GameUser.default.getInstance().beforeSeeVideoTime = 6e4 + new Date().getTime();
          cc.game.resume();
          $2AudioManager.AudioManager.getInstance().resumeAll();
          if (s && s.isEnded) {
            $2GameEvent.default.sendEvent("adevent", {
              type: "video",
              action: "finish",
              result: "success",
              adid: e
            });
            _this151.reportRewardBehavior(_t73, 4);
            console.log("视频播放完毕", s);
            var _o135 = n._rewardcomplat.get(e);
            if (null != _o135) {
              _o135(true);
              cc.director.emit("MYSELF_TOUCH_END", "playok");
            }
          } else {
            $2GameEvent.default.sendEvent("adevent", {
              type: "video",
              action: "finish",
              result: "fail",
              adid: e
            });
            _this151.reportRewardBehavior(_t73, 3);
            var _o136 = n._rewardcomplat.get(e);
            if (null != _o136) {
              _o136(false);
              cc.director.emit("MYSELF_TOUCH_END", "playfail");
            }
            console.log("中途退出");
          }
          _this151.loadvideo(_t73.videoid);
        });
        this._rewardVideoAd.set(e, _t73);
      }
    }
  }, {
    key: "reportRewardBehavior",
    value: function reportRewardBehavior(e, t) {
      console.log("reportRewardBehavior");
      if (e.rewardValue) {
        var _n75 = {
          operation: t,
          currentShow: 0,
          strategy: 1,
          adunit: e.videoid,
          scenelD: 1,
          shareValue: e.shareValue,
          rewardValue: e.rewardValue
        };
        console.log("Reward上报:", _n75);
        e.reportShareBehavior(_n75);
      }
    }
  }, {
    key: "reportShareBehavior",
    value: function reportShareBehavior(e) {
      console.log("reportShareBehavior");
      if (e.shareValue) {
        var _t74 = {
          operation: 6,
          inviteUser: $2GameUser.default.getInstance().openid,
          strategy: 1,
          adunit: e.videoid,
          scenelD: 1,
          shareValue: 1,
          rewardValue: 0
        };
        console.log("Share上报:", _t74);
        e.reportShareBehavior(_t74);
        e.shareValue = null;
        e.rewardValue = null;
        console.log("destroy-=-=-=-=-=1", e.videoid);
        this.destroy(e.videoid);
      }
    }
  }, {
    key: "destroy",
    value: function destroy(e) {
      console.log("destroy-=-=-=-=-=", e);
      if (this._rewardVideoAd.has(e)) {
        console.log("destroy1", e);
        var _t75 = this._rewardVideoAd.get(e);
        _t75.offError();
        _t75.offClose();
        _t75.destroy();
        this._rewardVideoAd.delete(e);
        this._rewardcomplat.delete(e);
        _t75.destroy();
        console.log("销毁视频", e);
      }
    }
  }, {
    key: "loadvideo",
    value: function loadvideo(e) {
      var _this152 = this;
      console.log("loadvideo===:", e);
      return new Promise(function (t) {
        var n = _this152._rewardVideoAd.get(e);
        if (!n) {
          _this152.Create(e, null);
          n = _this152._rewardVideoAd.get(e);
        }
        console.log("shareValue:", n.shareValue);
        console.log("rewardValue:", n.rewardValue);
        console.log("loading:", n.loading);
        if (1 == n.shareValue || 1 == n.rewardValue) {
          console.log("已经有了");
          t(n);
        } else {
          console.log("重新load广告------------");
          n.load().then(function (o) {
            console.log("loadok++++++++++++++++++++:", o);
            $2GameEvent.default.sendEvent("adevent", {
              type: "video",
              action: "get",
              result: "success",
              adid: e
            });
            t(n);
          }).catch(function (t) {
            $2GameEvent.default.sendEvent("adevent", {
              type: "video",
              action: "get",
              result: "fail",
              adid: e
            });
            console.log("loaderr++++++++++++++:", t);
          });
        }
      });
    }
  }, {
    key: "playvideo",
    value: function playvideo(e, t) {
      var _this153 = this;
      console.log("playvideo:", this._showing);
      if (!this._showing) {
        this.Create(e, t);
        this.loadvideo(e).then(function (n) {
          console.log("获得的结果是：shareValue", n.shareValue);
          console.log("获得的结果是：rewardValue", n.rewardValue);
          if (1 == n.shareValue) {
            console.log("shareAppMessage");
            var _e84 = function _e84(e) {
              if (1 == e) {
                t && t(e);
                _this153.reportShareBehavior(n);
                _this153.loadvideo(n.videoid);
              }
            };
            $2MiniPlatform.default.Instance.shareAppMessage(_e84);
          } else {
            console.log("show video");
            n.show().then(function () {
              $2GameEvent.default.sendEvent("adevent", {
                type: "video",
                action: "show",
                result: "success",
                adid: e
              });
              _this153.reportRewardBehavior(n, 1);
              _this153._showing = true;
              cc.game.pause();
              $2AudioManager.AudioManager.getInstance().pauseAll();
            }).catch(function () {
              $2GameEvent.default.sendEvent("adevent", {
                type: "video",
                action: "show",
                result: "fail",
                adid: e
              });
              cc.game.resume();
            });
          }
        }).catch(function () {
          _this153._showing = false;
        });
      }
    }
  }], [{
    key: "Instance",
    value: function Instance() {
      _ctor._instance || (_ctor._instance = new _ctor());
      return _ctor._instance;
    }
  }]);
}();
exports.WX_VideoMgr = exp_WX_VideoMgr;
exp_WX_VideoMgr._instance = null;